package ru.kurganec.ifmo.netland;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: oo
 * Date: 13.07.11
 * Time: 0:37
 */
public class NetlandProtocolExplorer  extends Thread{
    // ===========================================================
    // Constants
    // ===========================================================


    // ===========================================================
    // Fields
    // ===========================================================
    private Socket socket;
    BufferedReader in ;
    PrintWriter out ;//= new PrintWriter(s)

    public NetlandProtocolExplorer() {
        try {
            this.socket = new Socket("127.0.0.1", 1539);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "Cp1251"));
            out = new PrintWriter(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            System.exit(11            );
        }

    }
    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================


    @Override
    public void run() {
        while (true) {
            try {
                while (in.ready()){
                    System.out.print((char)in.read());
                }
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }
    }

    // ===========================================================
    // Methods
    // ===========================================================
    public static void main(String[] args) {
        NetlandProtocolExplorer explorer = new NetlandProtocolExplorer();
        explorer.start();

        BufferedReader r= new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            try {
                String msg = r.readLine();
                explorer.out.write(msg);
                explorer.out.flush();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

        }

    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
